package com.example.videolib

import android.content.Context
import android.graphics.SurfaceTexture
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.Toast
import tv.danmaku.ijk.media.player.IMediaPlayer
import tv.danmaku.ijk.media.player.IjkMediaPlayer

class VideoPlayDoubleView : FrameLayout {
    private var mMainPlayer: VideoPlayerView
    private var mSecondPlayer: VideoPlayerView
    private var mControlView: ProgressControlView
    private var mLoadFinish = 0

    private var isVerticalScreen = true

    //  视频实际宽高
    private var mVideoHeight: Float = 0f
    private var mVideoWidth: Float = 0f

    //  视频最大宽高
    private var mMaxVideoHeight: Float = 0f
    private var mMaxVideoWidth: Float = 0f

    //是否开启debug
    private var mEretcType = -1

    //    宽高比例  height/width
    private var mRatio: Float = 1f

    private var viewWidth = 0
    private var viewHeight = 0

    //初始屏幕比例为1 最小为0.5
    private var mViewProportion = 1.0f


    private var mMainMarginTop = 0f
    private var mMainMarginStart = 0f
    private var mSecondMarginTop = 0f
    private var mSecondMarginStart = 0f


    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) :
            super(context, attrs, defStyleAttr) {
        LayoutInflater.from(context).inflate(R.layout.view_layout_video_double_player, this, true)
        mMainPlayer = findViewById(R.id.view_video_player_double_main_vpv)
        mSecondPlayer = findViewById(R.id.view_video_player_double_second_vpv)
        mControlView = findViewById(R.id.view_video_player_double_control_pcv)

        mControlView.playListener = object : ProgressControlView.OnPlayListener {
            override fun start() {
                synchronized(this) {
                    mMainPlayer.start()
                    mSecondPlayer.start()
                }
            }

            override fun pause() {
                synchronized(this) {
                    mMainPlayer.pause()
                    mSecondPlayer.pause()
                }
            }

            override fun circulation() {

            }

            override fun moveto(index: Long) {
                synchronized(this) {
                    mMainPlayer.seekTo(index)
                    mSecondPlayer.seekTo(index)
                }
            }

            override fun add() {
                if (mViewProportion >= 1f) {
                    Toast.makeText(context, "已经是最大了,无法增大了", Toast.LENGTH_LONG).show()
                    return
                }
                mViewProportion += 0.1f
                mVideoWidth = mMaxVideoWidth * mViewProportion
                mVideoHeight = mMaxVideoHeight * mViewProportion
                val moveWidth = mMaxVideoWidth * 0.05f
                val moveHeight = mMaxVideoHeight * 0.05f

                val mainParams = mMainPlayer.layoutParams
                //mainview  放大时不要超过上边界 不要超过下边界
                when {
                    mMainPlayer.x + mainParams.width + moveWidth > viewWidth / 2 -> {
                        mMainMarginStart = viewWidth / 2 - mVideoWidth
                    }
                    mMainPlayer.x - moveWidth < 0 -> {
                        mMainMarginStart = 0f
                    }
                    else -> {
                        mMainMarginStart -= moveWidth
                    }
                }
                when {
                    mMainPlayer.y + mainParams.height + moveHeight > viewHeight -> {
                        mMainMarginTop = viewHeight - mVideoHeight
                    }
                    mMainPlayer.y - moveWidth < 0 -> {
                        mMainMarginTop = 0f
                    }
                    else -> {
                        mMainMarginTop -= moveHeight
                    }
                }

                val secondParams = mSecondPlayer.layoutParams
                when {
                    mSecondPlayer.x + secondParams.width + moveWidth > viewWidth / 2 -> {
                        mSecondMarginStart = viewWidth / 2 - mVideoWidth
                    }
                    mSecondPlayer.x - moveWidth < 0 -> {
                        mSecondMarginStart = 0f
                    }
                    else -> {
                        mSecondMarginStart -= moveWidth
                    }
                }

                when {
                    mSecondPlayer.y + secondParams.height + moveHeight > viewHeight -> {
                        mSecondMarginTop = viewHeight - mVideoHeight
                    }
                    mSecondPlayer.y - moveWidth < 0 -> {
                        mSecondMarginTop = 0f
                    }
                    else -> {
                        mSecondMarginTop -= moveHeight
                    }
                }
                updateView()
            }

            override fun reduce() {
                if (mViewProportion <= 0.5) {
                    Toast.makeText(context, "已经是最小了,无法缩小了", Toast.LENGTH_LONG).show()
                    return
                }
                mViewProportion -= 0.1f
                mVideoWidth = mMaxVideoWidth * mViewProportion
                mVideoHeight = mMaxVideoHeight * mViewProportion
                val moveWidth = mMaxVideoWidth * 0.05f
                val moveHeight = mMaxVideoHeight * 0.05f
                mMainMarginStart += moveWidth
                mSecondMarginStart += moveWidth
                mMainMarginTop += moveHeight
                mSecondMarginTop += moveHeight
                updateView()
            }

            override fun expand() {
                if (mMainMarginStart - 10 < 0) {
                    return
                }
                mMainMarginStart -= 10
                mSecondMarginStart += 10
                updateView()

            }

            override fun narrow() {
                if ((mMainMarginStart + mVideoWidth + 10) > viewWidth / 2) {
                    return
                }
                mMainMarginStart += 10
                mSecondMarginStart -= 10
                updateView()
            }

            override fun up() {
                if (mMainMarginTop - 10 < 0) {
                    return
                }
                mMainMarginTop -= 10
                mSecondMarginTop -= 10
                updateView()
            }

            override fun down() {
                if ((mMainMarginTop + mVideoHeight + 10) > viewHeight) {
                    return
                }
                mMainMarginTop += 10
                mSecondMarginTop += 10
                updateView()

            }
        }
    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        viewWidth = measuredWidth
        viewHeight = measuredHeight
        isVerticalScreen = viewHeight > viewWidth
    }

    fun setPathAndLoadData(path: String) {
        mLoadFinish = 0
        mMainPlayer.listener = IMediaPlayer.OnPreparedListener {
            Log.d("============", "setPathAndLoadData: ")
            mLoadFinish++

            mControlView.setTotalLength(it.duration)

            val height = it.videoHeight.toFloat()
            val width = it.videoWidth.toFloat()
            mEretcType = when {
                height > width -> LinearLayout.VERTICAL //竖屏
                height < width -> LinearLayout.HORIZONTAL //横屏
                else -> 2 //正方形
            }
            mRatio = (height / width)
            changeVideoSize()
        }
        mSecondPlayer.listener = IMediaPlayer.OnPreparedListener { mLoadFinish++ }
        mSecondPlayer.refreshListener = object : VideoPlayerView.FrameRefreshListener {
            override fun onSurfaceTextureUpdated(
                surface: SurfaceTexture,
                ijkMediaPlayer: IjkMediaPlayer
            ) {
                mControlView.setProgress(ijkMediaPlayer.currentPosition)
            }
        }
        mMainPlayer.setPathAndLoadData(path)
        mSecondPlayer.setPathAndLoadData(path)
        mSecondPlayer.setVolume(0f, 0f)
    }

    //根据视频高度 调整view大小
    private fun changeVideoSize() {
        when (mEretcType) {
            LinearLayout.VERTICAL -> {
                if (!isVerticalScreen) {
                    mMaxVideoHeight = viewHeight.toFloat()
                    mMaxVideoWidth = viewHeight / mRatio
                }
            }
            LinearLayout.HORIZONTAL -> {
                if (!isVerticalScreen) {
                    mMaxVideoWidth = (viewWidth / 2).toFloat()
                    mMaxVideoHeight = viewWidth / 2 * mRatio
                }
            }
        }
        mVideoHeight = mMaxVideoHeight * mViewProportion
        mVideoWidth = mMaxVideoWidth * mViewProportion
        initVideo()
        updateView()
    }

    private fun initVideo() {
        when (mEretcType) {
            LinearLayout.VERTICAL -> {
                mMainMarginStart = (viewWidth / 2 - mVideoWidth) / 2
                mSecondMarginStart = (viewWidth / 2 - mVideoWidth) / 2
            }
            LinearLayout.HORIZONTAL -> {
                mMainMarginTop = (viewHeight - mVideoHeight) / 2
                mSecondMarginTop = (viewHeight - mVideoHeight) / 2
            }
        }
    }


    private fun updateView() {
        val mainParams = LayoutParams(mVideoWidth.toInt(), mVideoHeight.toInt())
        mainParams.width = mVideoWidth.toInt()
        mainParams.height = mVideoHeight.toInt()
        mainParams.topMargin = mMainMarginTop.toInt()
        mainParams.marginStart = mMainMarginStart.toInt()
        mMainPlayer.layoutParams = mainParams

        val secondParams = LayoutParams(mVideoWidth.toInt(), mVideoHeight.toInt())
        secondParams.width = mVideoWidth.toInt()
        secondParams.height = mVideoHeight.toInt()
        secondParams.topMargin = mSecondMarginTop.toInt()
        secondParams.marginStart = mSecondMarginStart.toInt()
        mSecondPlayer.layoutParams = secondParams
    }
}